package com.book.publishing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.publishing.entity.BookMessage;
import com.book.publishing.mapper.BookMessageMapper;
import com.book.publishing.service.BookMessageService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 孙鑫
 * @Description:  20173952
 */

@Service
public class BookMessageServiceImpl extends ServiceImpl<BookMessageMapper, BookMessage> implements BookMessageService {

    @Autowired
    BookMessageMapper bookMessageMapper;

    @Override
    public List<BookMessage> getAllType() {
        return bookMessageMapper.getAllType();
    }

    @Override
    public List<BookMessage> findBookMessage(BookMessage bookMessage) {
        QueryWrapper<BookMessage> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookMessage.getBookname())) {
            queryWrapper.eq("bookname", bookMessage.getBookname());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BookMessage> pageBookMessage(QueryRequest request, BookMessage bookMessage) {
        QueryWrapper<BookMessage> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookMessage.getBookname())) {
            queryWrapper.like("bookname", bookMessage.getBookname());
        }
        if (StringUtils.isNotEmpty(bookMessage.getType())) {
            queryWrapper.like("type", bookMessage.getType());
        }
        Page<BookMessage> bookMessageIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookMessageIPage, queryWrapper);
    }

    @Override
    public void addBookMessage(BookMessage bookMessage) {
        this.save(bookMessage);
    }

    @Override
    public void updateBookMessage(BookMessage bookMessage) {
        this.saveOrUpdate(bookMessage);
    }

    @Override
    public void deleteBookMessage(String[] bookMessage) {
        List<String> list = Arrays.asList(bookMessage);
        this.removeByIds(list);
    }

    @Override
    public List<BookMessage> askBookMessage(String[] bookMessage) {
        List<String> list = Arrays.asList(bookMessage);
        return (List<BookMessage>) this.listByIds(list);
    }
}